package mapEditor.view.resItemEdit
{
	import asset.mapEditorResItemPanelAsset;
	import asset.mapEditorResItemPanelToolAsset;
	
	import fl.containers.ScrollPane;
	
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	import loader.LoaderBase;
	import loader.LoaderEvent;
	import loader.LoaderFactory;
	import loader.LoaderFlagType;
	import loader.LoaderQueue;
	import loader.LoaderType;
	import loader.URLLoaderBase;
	
	import mapEditor.MapEditorManager;
	import mapEditor.data.AssetTemplateInfo;
	import mapEditor.data.ResTemplateInfo;
	import mapEditor.events.MapEditorEvent;
	import mapEditor.view.assetEdit.AssetItemView;
	import mapEditor.view.assetEdit.AssetListView;
	import mapEditor.vo.AssetGroupVO;
	import mapEditor.vo.AssetVO;
	import mapEditor.vo.ResItemVO;
	
	import stages.StageReflection;
	
	import utils.DisplayUtil;

	public class ResItemPanelView extends mapEditorResItemPanelAsset
	{
		private var _editItemView:ResItemItemView;
		private var _selectItemView:ResItemItemView;
		private var _mapEditorResItemPanelToolAsset:mapEditorResItemPanelToolAsset;
		private var _scrollPane:ScrollPane;
		private var _resItemListView:ResItemListView;
		private var _assetListView:AssetListView;
		
		public function ResItemPanelView()
		{
			_mapEditorResItemPanelToolAsset=new mapEditorResItemPanelToolAsset();
			addChild(_mapEditorResItemPanelToolAsset);
			setView();
			addEvent();
		}
		
		private function setView():void
		{
			_resItemListView=new ResItemListView();
			_resItemListView.addEventListener(Event.SELECT, __onResItemListSelect);
			_scrollPane=new ScrollPane();
			_scrollPane.width=160;
			_scrollPane.source=_resItemListView;
			posPanel.addChild(_scrollPane);
			_mapEditorResItemPanelToolAsset.btnAdd.buttonMode=_mapEditorResItemPanelToolAsset.btnEdit.buttonMode=_mapEditorResItemPanelToolAsset.btnDel.buttonMode=true;
			resetView();
			tabItem.gotoAndStop(1);
			tabAsset.gotoAndStop(2);
		}
		
		private function addEvent():void
		{
			StageReflection.stage.addEventListener(Event.RESIZE, __onStageResize);
			_mapEditorResItemPanelToolAsset.btnAdd.addEventListener(MouseEvent.CLICK, __onBtnAddClick);
			_mapEditorResItemPanelToolAsset.btnEdit.addEventListener(MouseEvent.CLICK, __onBtnEditClick);
			_mapEditorResItemPanelToolAsset.btnDel.addEventListener(MouseEvent.CLICK, __onBtnDelClick);
			tabItem.addEventListener(MouseEvent.CLICK, __onTabClick);
			tabAsset.addEventListener(MouseEvent.CLICK, __onTabClick);
		}
		
		private function removeEvent():void
		{
			StageReflection.stage.removeEventListener(Event.RESIZE, __onStageResize);
			_mapEditorResItemPanelToolAsset.btnAdd.removeEventListener(MouseEvent.CLICK, __onBtnAddClick);
			_mapEditorResItemPanelToolAsset.btnEdit.removeEventListener(MouseEvent.CLICK, __onBtnEditClick);
			_mapEditorResItemPanelToolAsset.btnDel.removeEventListener(MouseEvent.CLICK, __onBtnDelClick);
			tabItem.removeEventListener(MouseEvent.CLICK, __onTabClick);
			tabAsset.removeEventListener(MouseEvent.CLICK, __onTabClick);
		}
		
		private function __onBtnEditClick(evt:MouseEvent):void
		{
			if(!_selectItemView)return;
			MapEditorManager.instance.selectResItemItem(_selectItemView.resItemVO);
		}
		
		private function __onBtnAddClick(evt:MouseEvent):void
		{
		}
		
		private function __onBtnDelClick(evt:MouseEvent):void
		{
		}
		
		private function __onResItemListSelect(evt:Event):void
		{
			_selectItemView=_resItemListView.selectItemView;
		}		
		
		private function __onTabClick(evt:MouseEvent):void
		{
			tabItem.gotoAndStop(2);
			tabAsset.gotoAndStop(2);
			
			evt.currentTarget.gotoAndStop(1);
			switch(evt.currentTarget)
			{
				case tabItem:
					if(_assetListView)
					{
						_assetListView.removeEventListener(Event.CHANGE, __onListViewChange);
						_assetListView.dispose();
						_assetListView=null;
					}
					if(_resItemListView)return;
					_resItemListView=new ResItemListView();
					_resItemListView.addEventListener(Event.SELECT, __onResItemListSelect);
					_resItemListView.addEventListener(Event.CHANGE, __onListViewChange);
					_scrollPane.source=_resItemListView;
					_scrollPane.refreshPane();
					_scrollPane.update();
					_mapEditorResItemPanelToolAsset.btnAdd.visible=_mapEditorResItemPanelToolAsset.btnEdit.visible=_mapEditorResItemPanelToolAsset.btnDel.visible=true;
					break;
				case tabAsset:
					if(_resItemListView)
					{
						_resItemListView.removeEventListener(Event.SELECT, __onResItemListSelect);
						_resItemListView.removeEventListener(Event.CHANGE, __onListViewChange);
						_resItemListView.dispose();
						_resItemListView=null;
					}
					if(_assetListView)return;
					_assetListView=new AssetListView();
					_assetListView.addEventListener(Event.CHANGE, __onListViewChange);
					_scrollPane.source=_assetListView;
					_scrollPane.refreshPane();
					_scrollPane.update();
					_mapEditorResItemPanelToolAsset.btnAdd.visible=_mapEditorResItemPanelToolAsset.btnEdit.visible=_mapEditorResItemPanelToolAsset.btnDel.visible=false;
					break;
			}
		}
		
		private function __onListViewChange(evt:Event):void
		{
			_scrollPane.refreshPane();
			_scrollPane.update();
		}
		
		private function __onStageResize(evt:Event):void
		{
			resetView();
		}
		
		private function resetView():void
		{
			_mapEditorResItemPanelToolAsset.y=StageReflection.stageHeight-_mapEditorResItemPanelToolAsset.height-90-20;
			setScrollPane();
		}
		
		private function setScrollPane():void
		{
			_scrollPane.height=StageReflection.stageHeight-_mapEditorResItemPanelToolAsset.height-90-20-2-20;
		}
		
		public function dispose():void
		{
			removeEvent();
			_editItemView=null;
			_selectItemView=null;
			if(_resItemListView)
			{
				_resItemListView.removeEventListener(Event.SELECT, __onResItemListSelect);
				_resItemListView.removeEventListener(Event.CHANGE, __onListViewChange);
				_resItemListView.dispose();
				_resItemListView=null;
			}
			if(_assetListView)
			{
				_assetListView.removeEventListener(Event.CHANGE, __onListViewChange);
				_assetListView.dispose();
				_assetListView=null;
			}
			DisplayUtil.dispose(_mapEditorResItemPanelToolAsset);
			_mapEditorResItemPanelToolAsset=null;
			DisplayUtil.dispose(_scrollPane);
			_scrollPane=null;		

		}
	}
}